// pages/writeEXP/writeEXP.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 本科学校类型
    studentTypes: [{
        value: '985',
        name: '985',
        check: ''
      },
      {
        value: '211',
        name: '211',
        check: ''
      },
      {
        value: '双非',
        name: '双非',
        check: ''
      },
      {
        value: '4',
        name: '其他',
        check: ''
      },
    ],
    // 本科毕业年份
    year: [{
        value: '2022',
        name: '2022',
      }, {
        value: '2021',
        name: '2021',
      },
      {
        value: '2020',
        name: '2020'
      },
      {
        value: '2019',
        name: '2019'
      },
      {
        value: '2018',
        name: '2018'
      },
      {
        value: '2017',
        name: '2017'
      },
      {
        value: '2016',
        name: '2016'
      },
      {
        value: '其他',
        name: '其他'
      },
    ],
    // 研究生毕业年份
    year2: [{
        value: '2022',
        name: '2022',
      }, {
        value: '2021',
        name: '2021',
      },
      {
        value: '2020',
        name: '2020'
      },
      {
        value: '2019',
        name: '2019'
      },
      {
        value: '2018',
        name: '2018'
      },
      {
        value: '2017',
        name: '2017'
      },
      {
        value: '2016',
        name: '2016'
      },
      {
        value: '其他',
        name: '其他'
      },
    ],
    // 考试类型分数
    scoresType: [{
        value: 'TOEFL',
        name: 'TOEFL',
      },
      {
        value: 'IELTS',
        name: 'IELTS',
      },
      {
        value: 'GRE',
        name: 'GRE',
      },
      {
        value: 'SUB',
        name: 'SUB',
      },
      {
        value: 'GMAT',
        name: 'GMAT',

      },
      {
        value: 'LSAT',
        name: 'LSAT',

      },
      {
        value: 'gkcj',
        name: '高考成绩',
      },
      {
        value: 'kycj',
        name: '考研成绩',
      },
    ],
    // 研究生学校类型
    schoolType: [{
        value: '985',
        name: '985',
      },
      {
        value: '211',
        name: '211',
      },
      {
        value: '双非',
        name: '双非',
      },
      {
        value: '4',
        name: '其他',
        check: ''
      },
    ],
    bkSchoolType: "985",
    bkEducationYear: "2021",
    bkSpecial: "",
    bkRank: "",
    yjsSchoolType: "",
    yjsSpecail: "",
    yjsRank: "",
    yjsEducationYear: "2021",
    scores: {
      "TOEFL": {},
      "IELTS": {},
      "GRE": {},
      "sub": {},
      "GMAT": {},
      "LSAT": {},
      "gkcj": {},
      "kycj": {}
    },
    ToeflScores: {
      'R': '',
      'L': '',
      'S': '',
      'W': '',
      'SUM': '',
    },
    IELTSScores: {
      'R': '',
      'L': '',
      'S': '',
      'W': '',
      'SUM': '',
    },
    GREScores: {
      'V': '',
      'Q': '',
      'AW': '',
    },
    SUBScores: {
      'PRO': '',
      'S': ''
    },
    GMATScores: {
      'Q': '',
      'V': '',
      'SUM': ''
    },
    LSAT: {
      'S': ''
    },
    GKCJ: {
      'JG': '',
      'SF': '',
      'Y': '',
      'S': '',
      'W': '',
      'ZH': '',
      'SUM': ''
    },
    KYCJ: {
      'Y': '',
      'Z': '',
      'S': '',
      'P1': '',
      'P2': '',
      'SUM': ''
    },
    select: false,
    tihuoWay: '选择SUB专业',
    id: ""
  },
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  mySelect(e) {
    var name = e.currentTarget.dataset.name;
    let getPro = `SUBScores.PRO`;
    this.setData({
      tihuoWay: name,
      select: false,
      [getPro]: name
    })
  },
  studentTypeRadioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const studentTypes = this.data.studentTypes;
    for (let i = 0, len = studentTypes.length; i < len; ++i) {
      studentTypes[i].checked = studentTypes[i].value === e.detail.value
    }
    this.setData({
      studentTypes: studentTypes
    })
    if (e.detail.value === "4") {
      this.setData({
        bkSchoolType: '',
      })
    } else {
      this.setData({
        bkSchoolType: e.detail.value,
      })
    }
  },
  // 毕业年份
  inSchool(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const year = this.data.year;
    for (let i = 0, len = year.length; i < len; ++i) {
      year[i].checked = year[i].value === e.detail.value
    }
    this.setData({
      bkEducationYear: e.detail.value,
      year: year,
    })
  },
  // 研究生年份
  outSchool(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const year = this.data.year2;
    for (let i = 0, len = year.length; i < len; ++i) {
      year[i].checked = year[i].value === e.detail.value
    }
    this.setData({
      yjsEducationYear: e.detail.value,
      year2: year,
    })
  },
  // 考试类型分数
  checkboxChange(e) {
    const that = this;
    const scoresType = this.data.scoresType;
    const values = e.detail.value;
    for (let i = 0, lenI = scoresType.length; i < lenI; ++i) {
      scoresType[i].checked = false;
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (scoresType[i].value === values[j]) {
          scoresType[i].checked = true;
          break;
        }
      }
    }
    this.setData({
      scoresType
    })
    for (let i = 0; i < scoresType.length; i++) {
      if (scoresType[i].checked == false) {

        switch (scoresType[i].name) {
          case 'TOEFL':
            that.setData({
              ToeflScores: {
                'R': '',
                'L': '',
                'S': '',
                'W': '',
                "SUM": ''
              }
            })
            break;
          case 'IELTS':
            that.setData({
              IELTSScores: {
                'R': '',
                'L': '',
                'S': '',
                'W': '',
                "SUM": ''
              }
            })
            break;
          case 'GRE':
            that.setData({
              GREScores: {
                'V': '',
                'Q': '',
                'AW': '',
              }
            })
            break;
          case 'SUB':
            that.setData({
              SUBScores: {
                'PRO': '',
                'S': ''
              },
            })
            break;
          case 'GMAT':
            that.setData({
              GMATScores: {
                'Q': '',
                'V': '',
                'SUM': ''
              }
            })
            break;
          case 'LSAT':
            that.setData({
              LSAT: {
                'S': ''
              },
            })
            break;
          case '高考成绩':
            that.setData({
              GKCJ: {
                '籍贯': '',
                '身份': '',
                'Y': '',
                'S': '',
                'W': '',
                'ZH': '',
                'SUM': ''
              }
            })
            break;
          case '考研成绩':
            that.setData({
              KYCJ: {
                'Y': '',
                'Z': '',
                'S': '',
                'P1': '',
                'P2': '',
                'SUM': ''
              },
            })
            break;
        }
      }
    }
  },
  // 计算TOEFL分数
  setScore: function(e) {
    let val = e.detail.value;
    let scores = this.data.scores;
    // TOEFL
    let getTOEFL = `scores.TOEFL`;
    let ToeflScores = this.data.ToeflScores;
    let getPro = e.target.dataset.type;
    let setUsaData = `ToeflScores.${getPro}`;

    let setSum = `ToeflScores.SUM`;
    let setSum2 = `scores.TOEFL.SUM`;
    this.setData({
      [setUsaData]: val,
    })
    let {
      R,
      L,
      S,
      W,
      SUM
    } = ToeflScores;
    // let sum = ((R - 0) + (L - 0) + (S - 0) + (W - 0));
    // this.setData({
    //   [setSum]: sum,
    //   [setSum2]: sum
    // })
    this.setData({
      [getTOEFL]: {
        R,
        L,
        S,
        W,
        SUM
      }
    })
  },
  // 计算IELTS分数  IELTSScores
  setIELTSScore: function(e) {
    let val = e.detail.value;
    let IELTSScores = this.data.IELTSScores;
    let getPro = e.target.dataset.type;
    let setUsaData = `IELTSScores.${getPro}`;
    let setSum = `IELTSScores.SUM`;
    let setSum2 = `scores.IELTS.SUM`;
    this.setData({
      [setUsaData]: val
    })
    let {
      R,
      L,
      S,
      W,
      SUM
    } = IELTSScores;
    // let sum = ((R - 0) + (L - 0) + (S - 0) + (W - 0));
    // console.log('sum', sum)
    // this.setData({
    //   [setSum]: sum,
    //   [setSum2]: sum
    // })
  },
  // 计算GRE分数 GREScores
  setGREScores: function(e) {
    let val = e.detail.value;
    let GREScores = this.data.GREScores;
    let getPro = e.target.dataset.type;
    let setUsaData = `GREScores.${getPro}`;
    this.setData({
      [setUsaData]: val
    })
  },
  // 计算SUB分数 setSUB
  setSUB: function(e) {
    let val = e.detail.value;
    let getPro = `SUBScores.S`;
    this.setData({
      [getPro]: val
    })
  },
  // 计算 GMATScores分数
  setGMATScores: function(e) {
    let val = e.detail.value;
    let GMATScores = this.data.GMATScores;
    let getPro = e.target.dataset.type;
    let setUsaData = `GMATScores.${getPro}`;
    let setSum = `GMATScores.SUM`;
    let setSum2 = `scores.GMAT.SUM`;
    this.setData({
      [setUsaData]: val
    })
    let {
      Q,
      V,
      SUM
    } = GMATScores;
    // let sum = ((Q - 0) + (V - 0));
    // this.setData({
    //   [setSum]: sum,
    //   [setSum2]: sum
    // })
  },
  // 计算LSAT 分数
  setLAST: function(e) {
    let val = e.detail.value;
    let getPro = `LAST.S`;
    this.setData({
      [getPro]: val
    })
  },
  // 计算高考成绩 
  setGKScores: function(e) {
    let val = e.detail.value;
    let GKCJ = this.data.GKCJ;
    let getPro = e.target.dataset.type;
    let setUsaData = `GKCJ.${getPro}`;
    let setSum = `GKCJ.SUM`;
    let setSum2 = `scores.GKCJ.SUM`;
    this.setData({
      [setUsaData]: val
    })
    let {
      Y,
      S,
      W,
      ZH,
      SUM
    } = GKCJ;
    // let sum = ((Y - 0) + (S - 0) + (W - 0) + (ZH - 0));
    // this.setData({
    //   [setSum]: sum,
    //   [setSum2]: sum
    // })
  },
  // 计算考研成绩
  setKYScores: function(e) {
    let val = e.detail.value;
    let KYCJ = this.data.KYCJ;
    let getPro = e.target.dataset.type;
    let setUsaData = `KYCJ.${getPro}`;
    let setSum = `KYCJ.SUM`;
    let setSum2 = `scores.KYCJ.SUM`;
    this.setData({
      [setUsaData]: val
    })
    let {
      Y,
      Z,
      S,
      P1,
      P2,
      SUM
    } = KYCJ;
    // let sum = ((Y - 0) + (Z - 0) + (S - 0) + (P1 - 0) + (P2 - 0));
    // this.setData({
    //   [setSum]: sum,
    //   [setSum2]: sum
    // })
  },
  // 研究生学校类型  选择框事件
  schoolType(e) {
    const schoolType = this.data.schoolType;
    for (let i = 0, len = schoolType.length; i < len; ++i) {
      schoolType[i].checked = schoolType[i].value === e.detail.value
    }
    this.setData({
      schoolType: schoolType
    })
    if (e.detail.value !== "4") {
      this.setData({
        yjsSchoolType: e.detail.value
      })
    } else {
      this.setData({
        yjsSchoolType: ''
      })
    }
  },
  // 研究生学校类型  输入框事件
  setYJSSchool: function(e) {
    this.setData({
      yjsSchoolType: e.detail.value
    })
  },
  //将score存到idnex
  interviewSubmit: function(e) {
    var that = this.data;
    this.setData({
      scores: {
        "TOEFL": that.ToeflScores,
        "IELTS": that.IELTSScores,
        "GRE": that.GREScores,
        "SUB": that.SUBScores,
        "GMAT": that.GMATScores,
        "LSAT": that.LSAT,
        "GKCJ": that.GKCJ,
        "KYCJ": that.KYCJ
      }
    });
    that = this.data;
    var standerSorce = {
      bkSchoolType: that.bkSchoolType,
      bkEducationYear: that.bkEducationYear,
      bkSpecial: that.bkSpecial,
      bkRank: that.bkRank,
      yjsSchoolType: that.yjsSchoolType,
      yjsSpecail: that.yjsSpecail,
      yjsRank: that.yjsRank,
      yjsEducationYear: that.yjsEducationYear,
      scores: "[" + JSON.stringify(that.scores) + "]",
      otherInstructions: "",
      leaveMessage: "",
      id: that.id
    }
    console.log(standerSorce);
    util.request(api.StanderSorceUpdate, JSON.stringify(standerSorce), "POST", true).then(function(res) {
      if (res.code === 200) {
        wx.setStorageSync('isReloadStanderSorce', true);
        wx.navigateBack({
          complete: (res) => {
            wx.showToast({
              title: '修改标化成绩成功',
              icon: 'none',
              duration: 1500
            })
          },
        })
      } else {
        wx.showToast({
          title: '修改标化成绩失败!',
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  // 修改本科学校类型
  setbkSchoolType: function(e) {
    this.setData({
      bkSchoolType: e.detail.value
    })
    console.log(this.data)
  },
  // 修改本科专业
  setbkSpecial: function(e) {
    this.setData({
      bkSpecial: e.detail.value
    })
  },
  // 修改本科成绩和算法排名
  setBkRank: function(e) {
    this.setData({
      bkRank: e.detail.value
    })
  },
  // 修改研究生专业
  setyjsSpecail: function(e) {
    this.setData({
      yjsSpecail: e.detail.value
    })
  },
  // 修改研究生成绩排名
  setyjsRank: function(e) {
    this.setData({
      yjsRank: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var standerSorce = options.standerSorce;
    const that = this;
    if (standerSorce) {
      var standerSorceObj = JSON.parse(standerSorce);
      //id
      that.setData({
        id: standerSorceObj.id
      })
      // 我的本科学校类型
      const getBkSchoolType = standerSorceObj.bkSchoolType;
      switch (getBkSchoolType) {
        case '985':
          that.setData({
            studentTypes: [{
                value: '985',
                name: '985',
                checked: 'true'
              }, {
                value: '211',
                name: '211'
              },
              {
                value: '双非',
                name: '双非'
              }, {
                value: '4',
                name: '其他'
              },
            ],
            bkSchoolType: getBkSchoolType
          });
          break;
        case '211':
          that.setData({
            studentTypes: [{
                value: '985',
                name: '985'
              }, {
                value: '211',
                name: '211',
                checked: 'true'
              },
              {
                value: '双非',
                name: '双非'
              }, {
                value: '4',
                name: '其他'
              },
            ],
            bkSchoolType: getBkSchoolType
          });
          break;
        case '双非':
          that.setData({
            studentTypes: [{
                value: '985',
                name: '985'
              }, {
                value: '211',
                name: '211'
              },
              {
                value: '双非',
                name: '双非',
                checked: 'true'
              }, {
                value: '4',
                name: '其他'
              },
            ],
            bkSchoolType: getBkSchoolType
          });
          break;
        default:
          that.setData({
            studentTypes: [{
                value: '985',
                name: '985'
              }, {
                value: '211',
                name: '211'
              },
              {
                value: '双非',
                name: '双非'
              }, {
                value: '4',
                name: '其他',
                checked: 'true'
              },
            ],
            bkSchoolType: getBkSchoolType
          });
      }

      // 本科毕业年份
      const getBkEducationYear = standerSorceObj.bkEducationYear;
      const getBkYear = that.data.year;
      for (let i = 0; i < getBkYear.length; i++) {
        if (getBkEducationYear == getBkYear[i].value) {
          let getYearItem = 'year[' + i + '].checked';
          that.setData({
            [getYearItem]: true
          })
        }
      }
      // 研究生毕业年份
      const getyjsEducationYear = standerSorceObj.yjsEducationYear;
      const getyjsYear = that.data.year2;
      for (let i = 0; i < getyjsYear.length; i++) {
        if (getyjsEducationYear == getyjsYear[i].value) {
          let getYearItem = 'year2[' + i + '].checked';
          that.setData({
            [getYearItem]: true
          })
        }
      }
      // 本科专业  本科成绩排行  研究生专业 研究生成绩排行
      const getBkSpecial = standerSorceObj.bkSpecial;
      const getBkRank = standerSorceObj.bkRank;
      const getyjsRank = standerSorceObj.yjsRank;
      const getyjsSpecail = standerSorceObj.yjsSpecail;
      that.setData({
        bkSpecial: getBkSpecial,
        bkRank: getBkRank,
        yjsSpecail: getyjsSpecail,
        yjsRank: getyjsRank
      })
      // 研究生学校类型
      const getyjsSchoolType = standerSorceObj.yjsSchoolType;
      const getyjsschool = that.data.schoolType;
      switch (getyjsSchoolType) {
        case '985':
          that.setData({
            schoolType: [{
                value: '985',
                name: '985',
                checked: 'true'
              }, {
                value: '211',
                name: '211'
              },
              {
                value: '双非',
                name: '双非'
              }, {
                value: '4',
                name: '其他'
              },
            ],
            yjsSchoolType: getyjsSchoolType
          });
          break;
        case '211':
          that.setData({
            schoolType: [{
                value: '985',
                name: '985'
              }, {
                value: '211',
                name: '211',
                checked: 'true'
              },
              {
                value: '双非',
                name: '双非'
              }, {
                value: '4',
                name: '其他'
              },
            ],
            yjsSchoolType: getyjsSchoolType
          });
          break;
        case '双非':
          that.setData({
            schoolType: [{
                value: '985',
                name: '985'
              }, {
                value: '211',
                name: '211'
              },
              {
                value: '双非',
                name: '双非',
                checked: 'true'
              }, {
                value: '4',
                name: '其他'
              },
            ],
            yjsSchoolType: getyjsSchoolType
          });
          break;
        default:
          that.setData({
            schoolType: [{
                value: '985',
                name: '985'
              }, {
                value: '211',
                name: '211'
              },
              {
                value: '双非',
                name: '双非'
              }, {
                value: '4',
                name: '其他',
                checked: 'true'
              },
            ],
            yjsSchoolType: getyjsSchoolType
          });
      }
      // 考试类型分数
      const getscores = standerSorceObj.scores;
      const scoresJson = JSON.parse(getscores);
      const getscoresType = that.data.scoresType;
      for (let i = 0; i < getscoresType.length; i++) {
        let getScoreItem = 'scoresType[' + i + '].checked';
        that.setData({
          [getScoreItem]: true
        })
      }
      that.setData({
        scores: {
          "TOEFL": scoresJson.TOEFL,
          "IELTS": scoresJson.IELTS,
          "GRE": scoresJson.GRE,
          "SUB": scoresJson.SUB,
          "GMAT": scoresJson.GMAT,
          "LSAT": scoresJson.LSAT,
          "GKCJ": scoresJson.GKCJ,
          "KYCJ": scoresJson.KYCJ
        },
        tihuoWay: scoresJson.SUB.PRO
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})